from urllib import request, parse
from http import cookiejar

# checkin_url = "http://101.43.106.219:8080/noabsence/actions/checkin"
# login_url = "http://101.43.106.219:8080/noabsence/actions/admin/login"

checkin_url = "http://101.43.106.219:8080/noabsence-testing/actions/checkin"
login_url = "http://101.43.106.219:8080/noabsence-testing/actions/admin/login"

jar = cookiejar.CookieJar()
handler = request.HTTPCookieProcessor(jar)
opener = request.build_opener(handler)

# 登录服务器
def login(cid, password):
    data = {"cid": cid, "passwd": password}
    data = parse.urlencode(data).encode("utf-8")
    req = request.Request(login_url, data=data)
    resp = opener.open(req)
    return b'ok' in resp.readlines()

# 使用学号和签到口令执行签到
def checkin(sid, password):
    data = {"sid": sid, "passwd": password}
    data = parse.urlencode(data).encode("utf-8")
    req = request.Request(checkin_url, data=data)
    resp = opener.open(req)
    return [i.decode("utf-8").strip() for i in resp.readlines()]
